package edu.pw.p2pclient.utils;

import java.util.Comparator;

import android.location.Location;
import edu.pw.p2pclient.networking.webservice.FoundResource;

public class DistanceComparator implements Comparator<FoundResource> {

	private Location mLocation;

	public DistanceComparator(double latitude, double longitude) {
		mLocation = new Location("Point A");
		mLocation.setLatitude(latitude);
		mLocation.setLongitude(longitude);
	}

	@Override
	public int compare(FoundResource lhs, FoundResource rhs) {
		double distL = distanceTo(lhs);
		double distR = distanceTo(rhs);
		if (distL < distR)
			return -1;
		else if (distL > distR)
			return 1;
		else
			return 0;
	}

	private double distanceTo(FoundResource res) {
		Location location = new Location("Point B");
		location.setLatitude(res.getLatitude());
		location.setLongitude(res.getLongitude());
		return mLocation.distanceTo(location);
	}

}
